/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */
package cn.eclass.dao;

import cn.eclass.entity.Entity;
import cn.eclass.entity.SchoolOrderEntity;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 *
 * @author fortruth
 */
public class SchoolOrderDao extends CommonDao {

    private static final String TABLE_NAME = "schoolorder";

    public SchoolOrderDao() {
        super(SchoolOrderDao.class.getName());
    }

    @Override
    protected Entity fillEntity(ResultSet resultSet) {
        SchoolOrderEntity entity = new SchoolOrderEntity();
        try {
            entity.setId(resultSet.getInt("ID"));
            entity.setAmount(resultSet.getFloat("AMOUNT"));
            entity.setClaimAmount(resultSet.getFloat("CLAIMAMOUNT"));
            entity.setThedate(resultSet.getTimestamp("THEDATE").getTime());
            entity.setValid(resultSet.getInt("STATUS"));
            entity.setUserId(resultSet.getInt("USER_ID"));
            entity.setSchoolId(resultSet.getInt("SCHOOL_ID"));
            entity.setOrderNo(resultSet.getString("ORDERNO"));
            entity.setPoints(resultSet.getInt("POINTS"));
            entity.setClassName(resultSet.getString("CLASSNAME"));
            entity.setUserName(resultSet.getString("USERNAME"));
            entity.setComment(resultSet.getString("COMMENT"));

        } catch (SQLException ex) {
            logger.error(ex);
        }
        return entity;
    }

    public int add(SchoolOrderEntity entity) {
        String sql = "insert into " + TABLE_NAME
                + "(`CLAIMAMOUNT`,`SCHOOL_ID`,`STATUS`,`POINTS`,`ORDERNO`, `CLASSNAME`, `USERNAME`,`COMMENT`, `USER_ID`) "
                + "values ("
                + entity.getClaimAmount() + ","
                + entity.getSchoolId() + ","
                + entity.getValid() + ","
                + entity.getPoints() + ","
                + "'" + entity.getOrderNo() + "',"
                + "'" + entity.getClassName() + "',"
                + "'" + entity.getUserName() + "',"
                + "'" + entity.getComment() + "',"
                + entity.getUserId()
                + ")";
        return super.insert(sql);
    }

    public boolean update(SchoolOrderEntity entity) {
        String sql = "update " + TABLE_NAME + " set "
                + "`AMOUNT` = " + entity.getAmount() + ","
                + "`CLAIMAMOUNT` = " + entity.getClaimAmount() + ","
                + "`SCHOOL_ID` = " + entity.getSchoolId() + ","
                + "`STATUS` = " + entity.getValid() + ","
                + "`USER_ID` = " + entity.getUserId() + ","
                + "`POINTS` = " + entity.getPoints() + ","
                + "`ORDERNO` = '" + entity.getOrderNo() + "',"
                + "`CLASSNAME` = '" + entity.getClassName() + "',"
                + "`COMMENT` = '" + entity.getComment() + "',"
                + "`USERNAME` = '" + entity.getUserName() + "'"
                + " where id = " + entity.getId();
        int effectedRows = super.update(sql);
        if (effectedRows > 0) {
            return true;
        } else {
            return false;
        }
    }

    public boolean delete(int id) {
        String sql = "delete from " + TABLE_NAME + " where id = " + id;
        int effectedRows = super.delete(sql);
        if (effectedRows > 0) {
            return true;
        } else {
            return false;
        }
    }

    public SchoolOrderEntity get(int id) {
        String sql = "select * from " + TABLE_NAME + " where id = " + id;
        List<SchoolOrderEntity> entitys = (List<SchoolOrderEntity>) super.select(sql);
        return entitys.get(0);
    }

    public List<SchoolOrderEntity> list() {
        String sql = "select * from " + TABLE_NAME;
        return (List<SchoolOrderEntity>) super.select(sql);
    }

    //Default order is by time.
    public List<SchoolOrderEntity> getByUser(int userId) {
        String sql = "select * from " + TABLE_NAME + " where user_id = " + userId + " ORDER BY THEDATE DESC";
        List<SchoolOrderEntity> list = (List<SchoolOrderEntity>) super.select(sql);
        return list;
    }
}
